package cn.com.generaldata.os.service.indicator_evaluation.incr_analysis;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 海洋资本存量增长速度评估指标计算类.
 */
public class MarineCapitalStockGrowthRateCalculateService extends IndicatorEvaluationCalculateService {

	private String marineCapitalStockIndicatorName = "海洋资本存量";

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double marineCapitalStock = getMarineCapitalStock(indicatorAccountVO, indicatorAccountVO.getYear());
		Double lastMarineCapitalStock = getMarineCapitalStock(indicatorAccountVO, indicatorAccountVO.getYear() - 1);

		Validate.isTrue(lastMarineCapitalStock != null, "上期海洋资本存量，不可以计算");
		return (marineCapitalStock - lastMarineCapitalStock) / lastMarineCapitalStock;
	}

	/**
	 * 获得的海洋资本存量.
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getMarineCapitalStock(IndicatorAccountVO indicatorAccountVO, int year) {

		Integer indicatorId = getIndicatorEvaluationId(marineCapitalStockIndicatorName, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, year, indicatorId);
		return getEvaluationIndicatorValue(cri);
	}

}
